package com.abc.controller;

import com.abc.dao.PlanMapper;
import com.abc.dao.base.PlanBaseMapper;
import com.abc.entity.Plan;
import com.abc.vo.Json;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "/plan", tags = "策划")
@RestController
@RequestMapping("/api/v1/plan")
public class PlanController {

    @Autowired
    PlanBaseMapper planBaseMapper;

    @Autowired
    PlanMapper planMapper;

    @GetMapping("list")
    public Json list() {
        List<Plan> list = planBaseMapper.queryPlan(null);
        return Json.succ("list", list);
    }


    @PostMapping("update")
    public Json update(@RequestBody Plan plan) {
        Integer count = planMapper.updateById(plan);
        if (count > 0) {
            return Json.succ("update");
        }
        return Json.fail("update");
    }

    @PostMapping("add")
    public Json add(@RequestBody Plan plan) {
        EntityWrapper wrapper = new EntityWrapper();
        wrapper.setEntity(plan);
        Integer count = planMapper.selectCount(wrapper);
        //策划只能6个
        if (count >= 6) {
            return Json.fail("add");
        }
        int c = planBaseMapper.insertPlan(plan);
        if (c > 0) {
            return Json.succ("add");
        } else {
            return Json.fail("add");
        }
    }


    @PostMapping("del")
    public Json del(@RequestBody Plan plan) {
        planMapper.deleteById(plan.getId());
        return Json.succ("del");
    }

}
